﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ETRobocon.RaceTraking.GameManagement
{
    public class ListContest : List<ERContest>
    {
        public void UpdateContents(ETRoboConTrakingSystem.ETRoboConRM.ETRoboConRaceMgmtClient raceMgmt)
        {
//            WSRaceMgmt.ContestInfo[] infos;
            ETRoboConTrakingSystem.ETRoboConRM.ContestInfo[] infos;
            if (raceMgmt.GetContestInfo(out infos))
            {
                foreach (var info in infos)
                {
                    ERContest contest = new ERContest(info.ContestId, info.Name);
//                    WSRaceMgmt.GameInfo[] gameInfos;
  //                  if (raceMgmt.GetGameInfo(contest.ID, out gameInfos))
                    ETRoboConTrakingSystem.ETRoboConRM.GameInfo[] gameInfos;
                    if (raceMgmt.GetGameInfo(out gameInfos, contest.ID))
                    {
                        foreach (var gameInfo in gameInfos)
                        {
                            ERGame game = new ERGame(gameInfo.GameId, gameInfo.Name, contest);
                            ERRound round = new ERRound(1,game);
                            game.Rounds.Add(round);
                            round = new ERRound(2,game);
                            game.Rounds.Add(round);
                            contest.Games.Add(game);
                        }
                    }
                    Add(contest);
                }
            }
        }
    }

    public class ERIDNamedElem
    {
        private int _id;
        private string _name;
        public ERIDNamedElem()
        {
        }
        public ERIDNamedElem(string name)
        {
            _name = name;
        }
        public ERIDNamedElem(int id, string name)
        {
            _id = id;
            _name = name;
        }

        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        public string Name {
            set { _name = value; }
            get { return _name; }
        }
    }

    public class ERContest : ERIDNamedElem
    {
        private List<ERGame> _games=new List<ERGame>();
        public ERContest()
        {
        }
        public ERContest(int id, string name) : base(id,name)
        {
        }

        public List<ERGame> Games { get { return _games; } }
    }

    public class ERGame : ERIDNamedElem
    {
        private List<ERRound> _rounds = new List<ERRound>();
        private ERContest _parent;

        public ERGame()
        {
        }

        public ERGame(int id, string name, ERContest parent)
            : base(id, name)
        {
            _parent = parent;
        }

        public ERContest Parent
        {
            set { _parent = value; }
            get { return _parent; }
        }
        public List<ERRound> Rounds { get { return _rounds; } }
    }

    public class ERRound : ERIDNamedElem
    {
        private List<ERTurn> _turns=new List<ERTurn>();
        private ERGame _parent;

        public ERRound()
        {
        }
        public ERRound(int no, ERGame parent)
            : base(no.ToString())
        {
            _parent = parent;
        }
        public ERRound(int id, int no) : base (id,no.ToString())
        {
            _turns = new List<ERTurn>();
        }

        public ERGame Parent {
            set { _parent = value; }
            get { return _parent; }
        }
        public int RoundNo {
            set { Name = value.ToString(); }
            get { return Int32.Parse(Name); }
        }
        public List<ERTurn> Turns { get { return _turns; } }
    }

    public class ERTurn : ERIDNamedElem
    {
        private ERTeamEntry _inCR;
        private ERTeamEntry _outCR;
        private ERRound _parent;

        public ERTurn()
        {
        }

        public ERTurn(int id, int no, ERTeamEntry inCR, ERTeamEntry outCR, ERRound parent)
            : base(id, no.ToString())
        {
            _parent = parent;
            _inCR = inCR;
            _outCR = outCR;
        }

        public int TurnNo
        {
            get { return Int32.Parse(Name); }
            set { Name = value.ToString(); }
        }
        public ERTeamEntry InCourceRunner {
            set { _inCR = value; }
            get { return _inCR; }
        }
        public ERTeamEntry OutCourceRunner {
            set { _outCR = value; }
            get { return _outCR; }
        }
    }

    public class ERTeamEntry : ERIDNamedElem
    {
        private int _bib;
        private int _runTime;
        private List<ERBonusResult> _bonusResults = new List<ERBonusResult>();

        public ERTeamEntry()
        {
        }
        public ERTeamEntry(int id, string name, int bib)
            : base(id, name)
        {
            _bib = bib;
        }

        public int Bib
        {
            set { _bib = value; }
            get { return _bib; }
        }
        public int RunTIme
        {
            set { _runTime = value; }
            get { return _runTime; }
        }
        public List<ERBonusResult> BonusResults
        {
            get { return _bonusResults; }
        }
    }

    public class ERBonusResult : ERIDNamedElem
    {
        private bool _cleared;

        public ERBonusResult()
        {
            _cleared = false;
        }
        public ERBonusResult(int id, string name)
            : base(id, name)
        {
            _cleared = false;
        }

        public bool Cleared
        {
            set { _cleared = value; }
            get { return _cleared; }
        }
    }

    public class EntryTeamRunTimeNotifier : INotifyPropertyChanged
    {
        private ERTeamEntry _target;

        public event PropertyChangedEventHandler PropertyChanged;

        public EntryTeamRunTimeNotifier(ERTeamEntry target)
        {
            _target = target;
        }
        public ERTeamEntry Target
        {
            get { return _target; }
            set { _target = value; }
        }

        public string RunTime
        {
            set
            {
                int v = Int32.Parse(value);
                _target.RunTIme = v;
                OnPropertyChanged("RunTime");
            }
            get
            {
                return _target.RunTIme.ToString();
            }
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }

}
